﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Fin;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;
using ESOL.WinUI.UICommon;
using ESOL.WinUI.Finance;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainCashFlow : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        private FinCashFlowEntity mSelectedCashFlow;
        private FinCashFlowEntity mOldCashFlow;
        public ApproveType ThisApproveType { get; set; }
        #endregion

        #region Constructor
        public frmMaintainCashFlow(FinCashFlowEntity entity)
        {
            InitializeComponent();
            mSelectedCashFlow = entity;
            mOldCashFlow = entity.Copy<FinCashFlowEntity>();
        }
        public frmMaintainCashFlow(MstPersonEntity person)
        {
            InitializeComponent();
            mSelectedPerson = person;
        }
        public frmMaintainCashFlow()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetPersonFields()
        {
            txtPerson.Text = "";
            if (mSelectedPerson == null)
            {
                return;
            }
            txtPerson.Text = mSelectedPerson.NameCn + " (" + mSelectedPerson.PersonCode + ")";
        }
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItemWithCode(cmbCashFlowType, "CFT", false);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbCurrency, "CS", false);
        }
        private void SetFields()
        {
            if (mSelectedCashFlow == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(mSelectedCashFlow.PeronId) == false)
            {
                mSelectedPerson = MstPersonBs.GetMstPersonByID(mSelectedCashFlow.PeronId);
                SetPersonFields();
            }
            if (mSelectedCashFlow.CashFlowDate.ToString("yyyy-MM-dd") == "1900-01-01" ||
                 mSelectedCashFlow.CashFlowDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                dtpCashFlowDate.EditValue = null;
            }
            else
            {
                dtpCashFlowDate.EditValue = mSelectedCashFlow.CashFlowDate;
            }
            txtAccountsCode.Text = "";
            txtAccountsCode.Tag = null;
            if (string.IsNullOrEmpty(mSelectedCashFlow.AccountsCode) == false)
            {
                FinAccountsEntity account = FinAccountsBs.GetByCode(mSelectedCashFlow.AccountsCode);
                if (account != null)
                {
                    txtAccountsCode.Text = account.AccountsName;
                    txtAccountsCode.Tag = account;
                }
            }
            cmbCashFlowType.SetSelectedValue(mSelectedCashFlow.CashFlowType);
            cmbCurrency.SetSelectedValue(mSelectedCashFlow.Currency);
            txtAmount.Text = mSelectedCashFlow.Amount.ToString("#0.00");
            txtRemark.Text = mSelectedCashFlow.Remark;

            UICommonFunctions.SetTextBoxDate(txtCreatedDate, mSelectedCashFlow.CreatedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, mSelectedCashFlow.CreatedBy);
            UICommonFunctions.SetTextBoxDate(txtLastModifiedDate, mSelectedCashFlow.LastModifiedDate, "yyyy-MM-dd hh:mm:ss");
            UICommonFunctions.SetTextBoxPersonName(txtLastModifiedBy, mSelectedCashFlow.LastModifiedBy);
        }
        private void SetEntity()
        {
            if (mSelectedCashFlow == null)
            {
                mSelectedCashFlow = new FinCashFlowEntity();
                mSelectedCashFlow.CashFlowId = Guid.NewGuid().ToString();
            }
            if (mSelectedPerson != null)
            {
                mSelectedCashFlow.PeronId = mSelectedPerson.PersonId;
            }
            if (dtpCashFlowDate.EditValue!=null)
            {
                mSelectedCashFlow.CashFlowDate = Convert.ToDateTime(dtpCashFlowDate.EditValue.ToString());
            }
            if (txtAccountsCode.Tag != null)
            {
                mSelectedCashFlow.AccountsCode = ((FinAccountsEntity)txtAccountsCode.Tag).AccountsCode;
            }
            if (cmbCashFlowType.SelectedValue != null)
            {
                mSelectedCashFlow.CashFlowType = cmbCashFlowType.SelectedValue.ToString();
            }
            if (cmbCurrency.SelectedValue != null)
            {
                mSelectedCashFlow.Currency = cmbCurrency.SelectedValue.ToString();
            }
            mSelectedCashFlow.Amount = CommonFunctions.ToDecimal(txtAmount.Text);
            mSelectedCashFlow.Remark = txtRemark.Text;

            if (ThisActionType == ActionType.New)
            {
                mSelectedCashFlow.CreatedDate = DateTime.Now;
                mSelectedCashFlow.CreatedBy = CommonParameter.CurrentUser.PersonId;
            }
            if (ThisActionType == ActionType.New || ThisActionType == ActionType.Modify)
            {
                mSelectedCashFlow.LastModifiedDate = DateTime.Now;
                mSelectedCashFlow.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
            }
        }
        private bool CheckData()
        {
            if (mSelectedPerson == null)
            {
                MessageBox.Show("请选择一个现金流的相关人员！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpCashFlowDate.EditValue == null)
            {
                MessageBox.Show("日期不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtAmount.Text))
            {
                MessageBox.Show("金额不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbCashFlowType.SelectedValue == null)
            {
                MessageBox.Show("类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbCurrency.SelectedValue == null)
            {
                MessageBox.Show("币别不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = FinCashFlowBs.SaveCashFlow(mSelectedCashFlow, null);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = FinCashFlowBs.SaveCashFlow(mSelectedCashFlow, mOldCashFlow);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindComboBox();
            SetPersonFields();
            SetFields();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                mSelectedPerson = frm.SelectedPerson;
                SetPersonFields();
            }
            else
            {
                mSelectedPerson = null;
                SetPersonFields();
            }
        }

        private void txtAccountsCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainFinanceAccountsCodeQuery frm = new frmMaintainFinanceAccountsCodeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedAccount != null)
            {
                txtAccountsCode.Text = frm.SelectedAccount.AccountsName;
                txtAccountsCode.Tag = frm.SelectedAccount;
            }
            else
            {
                txtAccountsCode.Text = "";
                txtAccountsCode.Tag = null;
            }
        }
        #endregion

    }
}
